
var roomList = null;

function goRank() {
	goPage("/rank.html");
}

$(document).ready(function() {
	document.getElementById("btnGoRank").onclick = goRank;
    refreshBattleRoomList();
	$("#roomPass").bind('keydown',function(event) {
		if(event.keyCode == "13") {
			$("#btnEnter")[0].click();
		}
    });
});

function refreshBattleRoomList() {
	$.ajax({
		url:"get.hall",
		type:"post",
		dataType:"json",
		success:function(data) {
			if(!responseProcess(data)) {

				if(data.userheadPic != null) {
					$("#divMe").css("background-image", headPicPathname(data.userheadPic));
					$("#divMe").css("border-radius", "50%");
					$("#divMe").css("background-size", "cover");
				} else {
					$("#divMe").css("background-image", ztaLogoPathname());
					$("#divMe").css("border-radius", "50% 0% 50% 50%");
				}
				
				console.log(data.room_list);
				roomList = data.room_list;
				
				for(let roomInfo of data.room_list) {
					if(roomInfo != null) addRoomItem(
						roomInfo.roomId,
						roomInfo.roomTitle,
						roomInfo.roomLocked,
						roomInfo.hostHeadPic,
						roomInfo.challengerHeadPic
					);
				}
				addCreateRoomIteam();
			}
		}
	});
}

function showPasswordDialog(onOk) {
	$("#roomPass").val("");
	var dia = $("#divPassDialog");
	dia.removeClass("top150");
	dia.addClass("top50");
	$("#roomPass").focus();

	$("#btnEnter")[0].onclick = function() {
		dia.removeClass("top50");
		dia.addClass("top150");
		onOk($("#roomPass").val());
	};
	
	$("#btnCancel")[0].onclick = function() {
		dia.removeClass("top50");
		dia.addClass("top150");
	};
}

function enterRoom(room_Id) {
	console.log("enter room");
	if($("#room_" + room_Id + "_challengerHeadPic").hasClass("Locked")) {
		showPasswordDialog(function(pass) {
			if(pass != null && pass != "") $.ajax({
				url:"join.hall",
				type:"post",
				data:{
					roomId:room_Id,
					roomPass:sha256.hex(pass)
				},
				dataType:"json",
				success:function(data) {
					if(!responseProcess(data)) {
						if(data.type == "enterRoomFail")
							alert(";( 密码不对哟");
					}
				}
			});
		});
	} else {
		$.ajax({
			url:"join.hall",
			type:"post",
			data:{
				roomId:room_Id
			},
			dataType:"json",
			success:function(data) {
				responseProcess(data);
			}
		});
	}
}

function addRoomItem(id, title, locked, hostHead, challengerHead) {
	console.log("id:" + id + " title:" + title + " locked:" + locked + " hostHead:" + hostHead + " challengerHead:" + challengerHead);
	$("#divHallMain").append($("<div id=\"room_" + id + "\" class=\"roomItem\" onclick=\"enterRoom('" + id + "')\">" +
		  "<div id=\"room_" + id + "_hostHeadPic\" class=\"headPic Unlock\"></div>" +
			"<div class=\"roomItem_info\"><label class=\"lblVS\">VS</label>" + 
	        "<label class=\"lblRoomTitle\">“" + title + "”</label></div>" + 
          "<div id=\"room_" + id + "_challengerHeadPic\" class=\"headPic " + (locked? "Locked": "Unlock" ) + "\"></div>" +
	  "</div>"));
	
	if(hostHead != null) $("#room_" + id + "_hostHeadPic").css("background-image", headPicPathname(hostHead));
	if(challengerHead != null) $("#room_" + id + "_challengerHeadPic").css("background-image", headPicPathname(challengerHead));
}

function addCreateRoomIteam() {
	$("#divHallMain").append($("<div id=\"divCreateRoom\" class=\"roomItem\" style=\"flex-direction:column;justify-content:center;\" onclick=\"goBattleRoom()\">" +
        "<label style=\"cursor:pointer;\">新建房间</label>" +
        "<label style=\"cursor:pointer;\">Create New Room</label>" +
    "</div>"));
}